package com.yuexunit.mobilenurse.module.NursingDocuments.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.app.AlertDialog;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.yuexunit.mobilenurse.R;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.NursingDocuments.bean.SingleChoiceBean;
import com.yuexunit.mobilenurse.module.NursingDocuments.model.impl.DocumentRecordDetailModel;
import com.yuexunit.mobilenurse.module.NursingDocuments.presenter.impl.DocumentRecordDetailPresenter;
import com.yuexunit.mobilenurse.module.NursingDocuments.ui.view.IDocumentRecordDetailView;
import com.yuexunit.mobilenurse.widget.CustomDatePicker;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by work-jx on 2016/10/24.
 */
public class Act_DocumentADLAssessmentDetail extends KJActivity implements IDocumentRecordDetailView {

    @Bind(R.id.signinput_name)
    TextView signinputName;
    @Bind(R.id.signinput_bedno)
    TextView signinputBedno;
    @Bind(R.id.signinput_visitno)
    TextView signinputVisitno;

    @Bind(R.id.documentadlassessment_eat_edit)
    Button documentadlassessmentEatEdit;
    @Bind(R.id.documentadlassessment_bathe_edit)
    Button documentadlassessmentBatheEdit;
    @Bind(R.id.documentadlassessment_decorate_edit)
    Button documentadlassessmentDecorateEdit;
    @Bind(R.id.documentadlassessment_dress_edit)
    Button documentadlassessmentDressEdit;
    @Bind(R.id.documentadlassessment_controlshit_edit)
    Button documentadlassessmentControlshitEdit;
    @Bind(R.id.documentadlassessment_controlpee_edit)
    Button documentadlassessmentControlpeeEdit;
    @Bind(R.id.documentadlassessment_toilet_edit)
    Button documentadlassessmentToiletEdit;
    @Bind(R.id.documentadlassessment_transfer_edit)
    Button documentadlassessmentTransferEdit;
    @Bind(R.id.documentadlassessment_walk_edit)
    Button documentadlassessmentWalkEdit;
    @Bind(R.id.documentadlassessment_stairs_edit)
    Button documentadlassessmentStairsEdit;
    @Bind(R.id.documentadlassessment_selfcare_edit)
    Button documentadlassessmentSelfcareEdit;
    @Bind(R.id.documentadlassessment_illness_edit)
    Button documentadlassessmentIllnessEdit;
    @Bind(R.id.documentadlassessment_nursing_edit)
    Button documentadlassessmentNursingEdit;


    public String inpNo;//住院号
    public String recorder;//记录人员id

    private DocumentRecordDetailPresenter presenter;

    // 单选内容对象。
    private SingleChoiceBean eatSingleChoiceBean;
    private SingleChoiceBean batheSingleChoiceBean;
    private SingleChoiceBean decorateSingleChoiceBean;
    private SingleChoiceBean dressSingleChoiceBean;
    private SingleChoiceBean controlshitSingleChoiceBean;
    private SingleChoiceBean controlpeeSingleChoiceBean;
    private SingleChoiceBean toiletSingleChoiceBean;
    private SingleChoiceBean transferSingleChoiceBean;
    private SingleChoiceBean walkSingleChoiceBean;
    private SingleChoiceBean stairsSingleChoiceBean;
    private SingleChoiceBean selfcareSingleChoiceBean;
    private SingleChoiceBean illnessSingleChoiceBean;
    private SingleChoiceBean nursingSingleChoiceBean;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_documentadlassessmentdetail);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new DocumentRecordDetailPresenter(new DocumentRecordDetailModel(), this);
        inpNo = PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_VISITNO);
        recorder = PreferenceHelper.readString(aty, AppConfig.NURSE_INFO, AppConfig.NURSE_LOGID);

        // 初始化单选内容对象。
        eatSingleChoiceBean = new SingleChoiceBean("进食", new String[]{"10:完全独立", "5:需部分帮忙", "0:需极大帮忙"}, -1);
        batheSingleChoiceBean = new SingleChoiceBean("洗澡", new String[]{"5:完全独立", "0:需部分帮忙"}, -1);
        decorateSingleChoiceBean = new SingleChoiceBean("修饰", new String[]{"5:完全独立", "0:需部分帮忙"}, -1);
        dressSingleChoiceBean = new SingleChoiceBean("穿衣", new String[]{"10:完全独立", "5:需部分帮忙", "0:需极大帮忙"}, -1);
        controlshitSingleChoiceBean = new SingleChoiceBean("控制大便", new String[]{"10:完全独立", "5:需部分帮忙", "0:需极大帮忙"}, -1);
        controlpeeSingleChoiceBean = new SingleChoiceBean("控制小便", new String[]{"10:完全独立", "5:需部分帮忙", "0:需极大帮忙"}, -1);
        toiletSingleChoiceBean = new SingleChoiceBean("如厕", new String[]{"10:完全独立", "5:需部分帮忙", "0:需极大帮忙"}, -1);
        transferSingleChoiceBean = new SingleChoiceBean("床椅转移", new String[]{"15:完全独立", "10:需部分帮忙", "5:需极大帮忙", "0:完全依赖"}, -1);
        walkSingleChoiceBean = new SingleChoiceBean("平地行走", new String[]{"15:完全独立", "10:需部分帮忙", "5:需极大帮忙", "0:完全依赖"}, -1);
        stairsSingleChoiceBean = new SingleChoiceBean("上下楼梯", new String[]{"10:完全独立", "5:需部分帮忙", "0:需极大帮忙"}, -1);
        selfcareSingleChoiceBean = new SingleChoiceBean("自理能力分级", new String[]{"1:重度依赖", "2:中度依赖", "3:轻度依赖", "4:无需依赖"}, -1);
        illnessSingleChoiceBean = new SingleChoiceBean("病情等级", new String[]{"1:病危/抢救", "2:病重/病情不稳", "3:病情稳定/康复期"}, -1);
        nursingSingleChoiceBean = new SingleChoiceBean("护理等级", new String[]{"1:特级护理", "2:一级护理", "3:二级护理", "4:三级护理"}, -1);
    }

    @Override
    public void initWidget() {
        super.initWidget();
        signinputName.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_NAME));
        signinputBedno.setText(PreferenceHelper.readString(aty, AppConfig.CURRENT_PATIENT_INFO, AppConfig.CURRENT_PATIENT_BEDCODE));
        signinputVisitno.setText(inpNo);
    }

    @OnClick({R.id.titlebar_img_back, R.id.right_text, R.id.titlebar_img_menu,
            R.id.documentadlassessment_eat_edit,
            R.id.documentadlassessment_bathe_edit,
            R.id.documentadlassessment_decorate_edit,
            R.id.documentadlassessment_dress_edit,
            R.id.documentadlassessment_controlshit_edit,
            R.id.documentadlassessment_controlpee_edit,
            R.id.documentadlassessment_toilet_edit,
            R.id.documentadlassessment_transfer_edit,
            R.id.documentadlassessment_walk_edit,
            R.id.documentadlassessment_stairs_edit,
            R.id.documentadlassessment_selfcare_edit,
            R.id.documentadlassessment_illness_edit,
            R.id.documentadlassessment_nursing_edit})
    public void onClick(View view) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());
        switch (view.getId()) {
            case R.id.titlebar_img_back:
                finish();
                break;
            case R.id.right_text:
                buildValue();
                break;
            case R.id.titlebar_img_menu:
                buildValue();
                break;

            case R.id.documentadlassessment_eat_edit:
                showSingleAlertDialog(eatSingleChoiceBean, documentadlassessmentEatEdit);
                break;
            case R.id.documentadlassessment_bathe_edit:
                showSingleAlertDialog(batheSingleChoiceBean, documentadlassessmentBatheEdit);
                break;
            case R.id.documentadlassessment_decorate_edit:
                showSingleAlertDialog(decorateSingleChoiceBean, documentadlassessmentDecorateEdit);
                break;
            case R.id.documentadlassessment_dress_edit:
                showSingleAlertDialog(dressSingleChoiceBean, documentadlassessmentDressEdit);
                break;
            case R.id.documentadlassessment_controlshit_edit:
                showSingleAlertDialog(controlshitSingleChoiceBean, documentadlassessmentControlshitEdit);
                break;
            case R.id.documentadlassessment_controlpee_edit:
                showSingleAlertDialog(controlpeeSingleChoiceBean, documentadlassessmentControlpeeEdit);
                break;
            case R.id.documentadlassessment_toilet_edit:
                showSingleAlertDialog(toiletSingleChoiceBean, documentadlassessmentToiletEdit);
                break;
            case R.id.documentadlassessment_transfer_edit:
                showSingleAlertDialog(transferSingleChoiceBean, documentadlassessmentTransferEdit);
                break;
            case R.id.documentadlassessment_walk_edit:
                showSingleAlertDialog(walkSingleChoiceBean, documentadlassessmentWalkEdit);
                break;
            case R.id.documentadlassessment_stairs_edit:
                showSingleAlertDialog(stairsSingleChoiceBean, documentadlassessmentStairsEdit);
                break;
            case R.id.documentadlassessment_selfcare_edit:
                showSingleAlertDialog(selfcareSingleChoiceBean, documentadlassessmentSelfcareEdit);
                break;
            case R.id.documentadlassessment_illness_edit:
                showSingleAlertDialog(illnessSingleChoiceBean, documentadlassessmentIllnessEdit);
                break;
            case R.id.documentadlassessment_nursing_edit:
                showSingleAlertDialog(nursingSingleChoiceBean, documentadlassessmentNursingEdit);
                break;

        }
    }

    public void showSingleAlertDialog(final SingleChoiceBean singleChoiceBean, final Button view){
        new AlertDialog.Builder(this)
            .setTitle(singleChoiceBean.getTitle())
            .setSingleChoiceItems(singleChoiceBean.getItems(), singleChoiceBean.getCheckedItem(), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    singleChoiceBean.setCheckedItem(i);
                    view.setText(singleChoiceBean.getItems()[i].split(":")[0]);
                    dialogInterface.dismiss();
                }
            })
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    singleChoiceBean.setCheckedItem(-1);
                    view.setText("");
                }
            })
            .create()
            .show();
    }

    private void buildValue() {
        StringBuilder builder = new StringBuilder();
        if (!StringUtils.isEmpty(documentadlassessmentEatEdit.getText())) {
            builder.append("478" + ":" + documentadlassessmentEatEdit.getText());
        }

        if (!StringUtils.isEmpty(documentadlassessmentBatheEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",503:" + documentadlassessmentBatheEdit.getText());
            } else {
                builder.append("503:" + documentadlassessmentBatheEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentDecorateEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",528:" + documentadlassessmentDecorateEdit.getText());
            } else {
                builder.append("528:" + documentadlassessmentDecorateEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentDressEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",504:" + documentadlassessmentDressEdit.getText());
            } else {
                builder.append("504:" + documentadlassessmentDressEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentControlshitEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",505:" + documentadlassessmentControlshitEdit.getText());
            } else {
                builder.append("505:" + documentadlassessmentControlshitEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentControlpeeEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",506:" + documentadlassessmentControlpeeEdit.getText());
            } else {
                builder.append("506:" + documentadlassessmentControlpeeEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentToiletEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",507:" + documentadlassessmentToiletEdit.getText());
            } else {
                builder.append("507:" + documentadlassessmentToiletEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentTransferEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",508:" + documentadlassessmentTransferEdit.getText());
            } else {
                builder.append("508:" + documentadlassessmentTransferEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentWalkEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",509:" + documentadlassessmentWalkEdit.getText());
            } else {
                builder.append("509:" + documentadlassessmentWalkEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentStairsEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",510:" + documentadlassessmentStairsEdit.getText());
            } else {
                builder.append("510:" + documentadlassessmentStairsEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentSelfcareEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",527:" + documentadlassessmentSelfcareEdit.getText());
            } else {
                builder.append("527:" + documentadlassessmentSelfcareEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentIllnessEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",675:" + documentadlassessmentIllnessEdit.getText());
            } else {
                builder.append("675:" + documentadlassessmentIllnessEdit.getText());
            }
        }

        if (!StringUtils.isEmpty(documentadlassessmentNursingEdit.getText())) {
            if (builder.length() != 0) {
                builder.append(",676:" + documentadlassessmentNursingEdit.getText());
            } else {
                builder.append("676:" + documentadlassessmentNursingEdit.getText());
            }
        }



        Map<String, String> params = new HashMap<>();
        params.put("inpNo", inpNo);
        params.put("timestamp", System.currentTimeMillis() + "");
        params.put("timepoint", StringUtils.getDataTime("yyyy-MM-dd HH:mm:ss"));
        params.put("templateId", "44");
        params.put("signData", builder.toString());
        params.put("recorder", recorder);


        presenter.uploadRecord(params);

    }

    /**
     * 通过dispatchTouchEvent每次ACTION_DOWN事件中动态判断非EditText本身区域的点击事件，然后在事件中进行屏蔽。然后当点击不是EditText时候，进行隐藏键盘操作
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {

        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件\
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    public void uploadRecord() {
        finish();
    }
}
